package mobi.pooper.db;

import mobi.pooper.pictures.Picture;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PicsDbAdapterImpl implements IPicsDbAdapter{
	static final String DATABASE_TABLE = "pictures";

	// Database fields
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_SUCCESSES_NUMBER = "successes";
	public static final String KEY_FAILS_NUMBER = "fails";

	private static final String TAG = PicsDbAdapterImpl.class.getSimpleName();

	private Context context;
	private SQLiteDatabase database = null;
	private PicsDbHelper dbHelper = null;

	public PicsDbAdapterImpl(Context context) {
		this.context = context;
	}

	public IPicsDbAdapter open() throws SQLException {
		if(dbHelper == null) dbHelper = new PicsDbHelper(context);
		if(database == null) database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		if(dbHelper != null) dbHelper.close();
		dbHelper = null;
		database = null;
	}

	public long insertItem(Picture item) {
		ContentValues initialValues = createContentValues(item);
		Log.d(TAG, "insert item: " + item);

		return database.insertOrThrow(DATABASE_TABLE, null, initialValues);
	}

	public boolean updateItem(Picture pic) {
		ContentValues updateValues = createContentValues(pic);

		//          Log.e(TAG, database + " " + item);

		return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
				+ pic.getDbRowId(), null) > 0;
	}

	public boolean deleteItem(long rowId) {
		return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public void deleteAllRecords() {
		database.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
		dbHelper.onCreate(database);
	}

	public ICursor getAllRecords() {
		return new CursorImpl(database.query(DATABASE_TABLE, new String[] { 
				KEY_ROWID, KEY_NAME, KEY_DESCRIPTION, KEY_SUCCESSES_NUMBER, KEY_FAILS_NUMBER}, null, null, null, null, null));
	}


	public CursorImpl getRecord(long rowId) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_NAME, KEY_DESCRIPTION, KEY_SUCCESSES_NUMBER, KEY_FAILS_NUMBER}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return new CursorImpl(mCursor);
	}

	private ContentValues createContentValues(Picture pic) {
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, pic.getName());
		values.put(KEY_DESCRIPTION, pic.getDescription());
		values.put(KEY_SUCCESSES_NUMBER, pic.getSuccessesNumber());
		values.put(KEY_FAILS_NUMBER, pic.getFailsNumber());
		return values;
	}

	public Picture[] getAllItems() {

		ICursor cursor = getAllRecords();
		cursor.moveToFirst();
		Picture[] items = new Picture[cursor.getCount()];

		int titleIndex = cursor.getColumnIndexOrThrow(KEY_NAME);
		int descriptionIndex = cursor.getColumnIndexOrThrow(KEY_DESCRIPTION);
		int successesNumberIndex = cursor.getColumnIndexOrThrow(KEY_SUCCESSES_NUMBER);
		int failsNumberIndex = cursor.getColumnIndexOrThrow(KEY_FAILS_NUMBER);
		int rowIdIndex = cursor.getColumnIndexOrThrow(KEY_ROWID);

		for(int i = 0; i < items.length; i++){
			String name = cursor.getString(titleIndex);
			String description = cursor.getString(descriptionIndex);
			int successesNumber = cursor.getInt(successesNumberIndex); 
			int failsNumber = cursor.getInt(failsNumberIndex); 
			int rowId = cursor.getInt(rowIdIndex);

			items[i] = new Picture(name, description, successesNumber, failsNumber);
			items[i].setDbRowId(rowId);
			Log.d(TAG, "read item: " + items[i]);

			cursor.moveToNext();
		}
		return items;
	}
}
